package net.ariel.myideablog.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.ariel.myideablog.dao.IBlog;
import net.ariel.myideablog.dao.ICategory;
import net.ariel.myideablog.dao.ILink;
import net.ariel.myideablog.model.Blog;
import net.ariel.myideablog.model.Category;
import net.ariel.myideablog.model.Link;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class BlogController extends MultiActionController {

	private String viewPage;
	private ICategory iCategory;
	private ILink iLink;
	private IBlog iBlog;
	private int numberPerPage;
	private int pages;
	
	public void setViewPage(String viewPage) {
		this.viewPage = viewPage;
	}
	
	public void setICategory(ICategory iCategory) {
		this.iCategory = iCategory;
	}
	
	public void setILink(ILink iLink) {
		this.iLink = iLink;
	}
	
	public void setIBlog(IBlog iBlog) {
		this.iBlog = iBlog;
	}
	
	public void setNumberPerPage(int numberPerPage) {
		this.numberPerPage = numberPerPage;
	}
	
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
		int blogNumber = iBlog.getBlogNumber();
		if(numberPerPage != 0) {
			pages = blogNumber/numberPerPage;
			if(blogNumber % numberPerPage != 0) {
				pages++;
			}
		}
		else {
			pages = 0;
		}
		
		List<Category> categories = iCategory.getAllCategories();
		List<Link> links = iLink.getAllLinks();
		List<Blog> blogs = iBlog.getBlogsByPage(0, numberPerPage);

		ModelAndView mav = new ModelAndView();
		mav.setViewName(viewPage);
		mav.addObject("categories", categories);
		mav.addObject("links", links);
		mav.addObject("blogs", blogs);
		mav.addObject("pages", pages);
		return mav;
	}
	
	public ModelAndView page(HttpServletRequest request, HttpServletResponse response) {
		String page = request.getParameter("page");
		int pageNo = Integer.parseInt(page)-1;
		
		int blogNumber = iBlog.getBlogNumber();
		if(numberPerPage != 0) {
			pages = blogNumber/numberPerPage;
			if(blogNumber % numberPerPage != 0) {
				pages++;
			}
		}
		else {
			pages = 0;
		}
		
		List<Category> categories = iCategory.getAllCategories();
		List<Link> links = iLink.getAllLinks();
		List<Blog> blogs = iBlog.getBlogsByPage(pageNo*numberPerPage, (pageNo+1)*numberPerPage);

		ModelAndView mav = new ModelAndView();
		mav.setViewName(viewPage);
		mav.addObject("categories", categories);
		mav.addObject("links", links);
		mav.addObject("blogs", blogs);
		mav.addObject("pages", pages);
		return mav;
	}
	
	public ModelAndView category(HttpServletRequest request, HttpServletResponse response) {
		String categoryKey = request.getParameter("key");
		List<Category> categories = iCategory.getAllCategories();
		List<Link> links = iLink.getAllLinks();
		List<Blog> blogs = iBlog.getBlogsByCategory(categoryKey);
		
		ModelAndView mav = new ModelAndView();
		mav.setViewName(viewPage);
		mav.addObject("categories", categories);
		mav.addObject("links", links);
		mav.addObject("blogs", blogs);
		return mav;
	}
}
